/*
  ******************************************************************************
  * @file    apt32f172_led.h
  * @author  APT AE Team
  * @version V1.12
  * @date    2018/03/08
  ******************************************************************************
  *THIS SOFTWARE WHICH IS FOR ILLUSTRATIVE PURPOSES ONLY WHICH PROVIDES 
  *CUSTOMER WITH CODING INFORMATION REGARDING THEIR PRODUCTS.
  *APT CHIP SHALL NOT BE HELD RESPONSIBILITY ADN LIABILITY FOR ANY DIRECT, 
  *INDIRECT DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF 
  *SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION 
  *CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.AND APT CHIP RESERVES 
  *THE RIGHT TO MAKE CHANGES IN THE SOFTWARE WITHOUT NOTIFICATION
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/ 
#ifndef _apt32f172_led_H
#define _apt32f172_led_H

/* Includes ------------------------------------------------------------------*/
#include "apt32f172.h"

/******************************************************************************
************************** LED Registers Definition ****************************
******************************************************************************/

/******************************************************************************
* LEDCR: LED Control Regiser
******************************************************************************/
/*#define LIGHTON        (0x01ul << 0)

#define LEDCLK_DIV4    (0x00ul << 1)
#define LEDCLK_DIV8    (0x01ul << 1)
#define LEDCLK_DIV16   (0x02ul << 1)
#define LEDCLK_DIV32   (0x03ul << 1)
#define LEDCLK_DIV64   (0x04ul << 1)
#define LEDCLK_DIV128  (0x05ul << 1)
#define LEDCLK_DIV256  (0x06ul << 1)
#define LEDCLK_DIV512  (0x07ul << 1)

#define COMOM(val)     	(((val) & 0x01)<< 4)
#define SHAREMD(val)  	(((val) & 0x01)<< 7)
#define TKEYSCCNT(val)  (((val) & 0x07)<< 8)

#define COM_EN(val)    (((val) & 0xFF)<< 16)*/

/** Set CR register                                                          */
 #define CSP_LED_SET_CR(led, val)    ((led)->CR = (val))
/** Get CR register                                                          */
 #define CSP_LED_GET_CR(led)         ((led)->CR)

#define CSP_LED_SET_TIMCR(led, val)  ((led)->TIMCR = (val))

/******************************************************************************
* BRIGHT : LED Brigntness Control Register
******************************************************************************/
/** Set BRIGHT register                                                      */
 #define CSP_LED_SET_BRT(led, val)    ((led)->BRIGHT = (val))
/** Get BRIGHT register                                                      */
 #define CSP_LED_GET_BRT(led)         ((led)->BRIGHT)

/******************************************************************************
* Interrupt Related
******************************************************************************/
#define LED_ICEND       (0x01ul << 0)      /**< COM Scan Cycle End Interrupt */
#define LED_IPEND       (0x01ul << 1)      /**< All COM Scan End Interrupt */

/** Set IMCR register                                                      */
#define CSP_LED_SET_IMCR(led, val)    ((led)->IMCR = (val))
/** Get IMCR register                                                      */
#define CSP_LED_GET_IMCR(led)         ((led)->IMCR)


/** Get RISR register (Status)                                              */
#define CSP_LED_GET_RISR(led)         ((led)->RISR)

/** Get MISR register (Status)                                              */
#define CSP_LED_GET_MISR(led)         ((led)->MISR)

/** Set ICR register (Status)                                               */
#define CSP_LED_SET_ICR(led, val)     ((led)->ICR = (val))


/******************************************************************************
* LEDCR: LED Timing Control Regiser
******************************************************************************/

#define DCOMCNT(val)    (((val) & 0xFF))
#define NOVCNT(val)  	(((val) & 0xFF)<< 8)

/** Set TIMCR register                                                          */
 #define CSP_LED_SET_TIMCR(led, val)    ((led)->TIMCR = (val))
/** Get TIMCR register                                                          */
 #define CSP_LED_GET_TIMCR(led)         ((led)->TIMCR)

/******************************************************************************
* Segment Data Register
******************************************************************************/
#define LEDx_S0_ADDR(led) (&((led) -> SEGDATA0))
/** Set SEGDATAx register                                                    */
#define CSP_LED_SET_SEGDATAx(led,n,val)   (*(LEDx_S0_ADDR(led) + (4*n)) = (val))
/** Get SEGDATAx register                                                    */
#define CSP_LED_GET_SEGDATAx(led,n)       (*(LEDx_S0_ADDR(led) + (4*n)))

#define LED_0_S0_ADDR ((volatile unsigned int *)0x4006002C)
/** Set LED0 SEGDATAx register                                                    */
#define CSP_LED_0_SET_Sx(n,val)   (*(LED_0_S0_ADDR + (n)) = (val))
/** Get LED0 SEGDATAx register                                                    */
#define CSP_LED_0_GET_Sx(n)       (*(LED_0_S0_ADDR + (n)))

/******************************************************************************
******************** LED External Variables              *********************
******************************************************************************/
extern unsigned int segdat[8]; 
extern unsigned int led_bright;
extern U8_T ledsync;

/******************************************************************************
******************** LED External Functions Declaration *********************
******************************************************************************/
void led_refresh(unsigned short blinktuning,U8_T blinkmask,unsigned short dimtuning,unsigned short syncnt);




/******************************************************************************
************************** LED0 Registers Definition ****************************
******************************************************************************/
#define		 LED0_CR_RST     			((CSP_REGISTER_T)0x00000000)		/**< CR  reset value  	*/
#define		 LED0_BRIGHT_RST            ((CSP_REGISTER_T)0x00000000)      	/**< BRIGHT  reset value*/
#define		 LED0_RISR_RST       		((CSP_REGISTER_T)0x00000000)  		/**< RISR reset value  	*/
#define		 LED0_IMCR_RST             	((CSP_REGISTER_T)0x00000000)		/**< IMCR   reset value */
#define		 LED0_MISR_RST              ((CSP_REGISTER_T)0x00000000)    	/**< MISR   reset value */
#define		 LED0_ICR_RST               ((CSP_REGISTER_T)0x00000000)    	/**< ICR   reset value  */
#define		 LED0_TIMCR_RST             ((CSP_REGISTER_T)0x00000000)		/**< TIMCR  reset value */
#define		 LED0_BLKER_RST             ((CSP_REGISTER_T)0x00000000)		/**< BLKER  reset value */
#define		 LED0_BLKDR_RST             ((CSP_REGISTER_T)0x00000000)		/**< BLKDR  reset value */
#define		 LED0_BLKST_RST             ((CSP_REGISTER_T)0x00000000)		/**< BLKST  reset value */
#define		 LED0_SEGDAT0_RST           ((CSP_REGISTER_T)0x00000000)     	/**< SEGDAT0  reset value*/
#define		 LED0_SEGDAT1_RST           ((CSP_REGISTER_T)0x00000000)   		/**< SEGDAT1  reset value*/
#define		 LED0_SEGDAT2_RST           ((CSP_REGISTER_T)0x00000000)    	/**< SEGDAT2  reset value*/
#define		 LED0_SEGDAT3_RST           ((CSP_REGISTER_T)0x00000000)  		/**< SEGDAT3  reset value*/
#define		 LED0_SEGDAT4_RST           ((CSP_REGISTER_T)0x00000000)     	/**< SEGDAT4  reset value*/
#define		 LED0_SEGDAT5_RST           ((CSP_REGISTER_T)0x00000000)   		/**< SEGDAT5  reset value*/
#define		 LED0_SEGDAT6_RST           ((CSP_REGISTER_T)0x00000000)    	/**< SEGDAT6  reset value*/
#define		 LED0_SEGDAT7_RST           ((CSP_REGISTER_T)0x00000000)  		/**< SEGDAT7  reset value*/

/******************************************************************************
************************* LED0 Registers reset value ************************
******************************************************************************/
 #define LIGHTON          ((CSP_REGISTER_T)(0x00000001ul))          //led  LIGHT ON
 
 /**
  * @brief  LED CLK Control
  */
typedef enum
{
		LEDCLK_DIV4  =  ((CSP_REGISTER_T)(0x00ul << 1)),
		LEDCLK_DIV8  =  ((CSP_REGISTER_T)(0x01ul << 1)),
		LEDCLK_DIV16 =  ((CSP_REGISTER_T)(0x02ul << 1)),
		LEDCLK_DIV32 =  ((CSP_REGISTER_T)(0x03ul << 1)),
		LEDCLK_DIV64 =  ((CSP_REGISTER_T)(0x04ul << 1)),
		LEDCLK_DIV128 = ((CSP_REGISTER_T)(0x05ul << 1)),
		LEDCLK_DIV256 = ((CSP_REGISTER_T)(0x06ul << 1)),
		LEDCLK_DIV512 = ((CSP_REGISTER_T)(0x07ul << 1))
}LED_LEDCLK_TypeDef;

/**
  * @brief  Mechanical Key Input Scan Enable
  */
typedef enum
{
	ENKEY_SCEN		=			((CSP_REGISTER_T)(0X01<<6)),	
	DISKEY_SCEN		=			((CSP_REGISTER_T)(0X00<<6))	
}LED_KEY_SCEN_TypeDef;

/**
  * @brief  LED Tkey share diable and enable
  */
typedef enum
{
	ENTkeyShare			=			((CSP_REGISTER_T)(0X01<<7)),	
	DISTkeyShare		=			((CSP_REGISTER_T)(0X00<<7))
}LED_TkeyShare_TypeDef;

/**
  * @brief  LED Bright set
  */
typedef enum
{
	LED_Bright_100			=			((CSP_REGISTER_T)(0X00)),	
	LED_Bright_87			=			((CSP_REGISTER_T)(0X01)),
	LED_Bright_75			=			((CSP_REGISTER_T)(0X02)),	
	LED_Bright_62			=			((CSP_REGISTER_T)(0X03)),
	LED_Bright_50			=			((CSP_REGISTER_T)(0X04)),	
	LED_Bright_37			=			((CSP_REGISTER_T)(0X05)),
	LED_Bright_25			=			((CSP_REGISTER_T)(0X06)),	
	LED_Bright_12			=			((CSP_REGISTER_T)(0X07))
}LED_Bright_TypeDef;

/**
  * @brief  LED INT
  */
  //MISR/IMCR/ICR/RISR
typedef enum
{
	ICEND				=			((CSP_REGISTER_T)(0X01<<0)),	
	IPEND				=			((CSP_REGISTER_T)(0X01<<1)),	
}LED_INT_TypeDef;	

/**
  * @brief  LED SEGDATX data selected
  */
typedef enum
{
	SEGDAT_NUM0				=			((CSP_REGISTER_T)(0X00)),	
	SEGDAT_NUM1				=			((CSP_REGISTER_T)(0X01)),	
	SEGDAT_NUM2				=			((CSP_REGISTER_T)(0X02)),
	SEGDAT_NUM3				=			((CSP_REGISTER_T)(0X03)),
	SEGDAT_NUM4				=			((CSP_REGISTER_T)(0X04)),	
	SEGDAT_NUM5				=			((CSP_REGISTER_T)(0X05)),	
	SEGDAT_NUM6				=			((CSP_REGISTER_T)(0X06)),
	SEGDAT_NUM7				=			((CSP_REGISTER_T)(0X07))
}LED_SEGDATX_TypeDef;

/**
  * @brief  LED TK share mode output mode selected
  */
typedef enum
{
	TkShareComOutPut_HighImpedance				=		((CSP_REGISTER_T)(0X00<<4)),
	TkShareComOutPut_High						=		((CSP_REGISTER_T)(0X01<<4))		
}LED_TkeyShare_ComOutputST_TypeDef;

/**
  * @brief  LED SEG selected
  */
typedef enum
{
	LED_SEG_0		=		1,
	LED_SEG_1		=		2,
	LED_SEG_2		=		3,
	LED_SEG_3		=		4,
	LED_SEG_4		=		5,
	LED_SEG_5		=		6,
	LED_SEG_6		=		7,
	LED_SEG_7		=		8,
}LED_SEG_X_TypeDef;  

/******************************************************************************
********************* ADC12 External Functions Declaration ********************
******************************************************************************/
extern void LED_RESET_VALUE(void);
extern void LED_SCAN_ENABLE(FunctionalStatus NewState);
extern void LED_COM_ENABLE(U8_T LED_COMx_ENABLE);
extern void LED_Control_Config(LED_LEDCLK_TypeDef LED_LEDCLK , U8_T LED_COM_EN , LED_Bright_TypeDef LED_Bright_X , U8_T DCOMCNT , U8_T NOVCNT  );
extern void LED_seg_io_initial(LED_SEG_X_TypeDef LED_SEG_X , U8_T LED_IO_G);
extern void LED_INT_CMD(LED_INT_TypeDef LED_INT_x , FunctionalStatus NewState);
extern void LED_SEGDATX_data_write(LED_SEGDATX_TypeDef SEGDATX , U8_T SEGDATX_data);
extern void LED_Int_Enable(void);
extern void LED_Int_Disable(void);

#endif   /**< apt32f172_led_H */
/******************* (C) COPYRIGHT 2018 APT Chip *****END OF FILE****/
